<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <common-head></common-head>
  </div>
  <script src="./vue.js"></script>
  <script>
    // 定义第三方实例
    const bus = new Vue();
    const CommonHead = {
      template: `
       <div>
         <h2>这是一个父组件</h2> 
         <hr/>
        <child1></child1>
        <hr/>
        <child2></child2>
       </div>
     `,
      components: {
        Child1: {
          template: `
            <div>
              <h3>这是一个子组件1</h3>  
              <button @click="send">向我的兄弟发送一个数据</button>
            </div>
        `,
          data() {
            return {
              msg: '子组件的数据',
            }
          },
          methods: {
            send() {
              bus.$emit('biubiu', this.msg);
            }
          }
        }
        ,Child2: {
          template: `
            <div>
              <h3>我是另一个子组件</h3> 
              {{ msg }} 
            </div>
          `,
          data(){
            return {
              msg: ''
            }
          },
          mounted(){
            // 使用组件会自动触发
           /*  bus.$on('biubiu', (msg) => {
              this.msg = msg;
            }) */
            /* const _this = this;
            bus.$on('biubiu', function(msg){
              _this.msg = msg;
            }) */
            
            bus.$on('biubiu', function(msg){
              this.msg = msg;
            }.bind(this))
          }
        }
      }
    }
    Vue.component('CommonHead', CommonHead);
    // 相当于定义一个 组件构造函数 VueComponent(继承Vue)
    const vm = new Vue({
      el: '#app'
    })
  </script>
</body>

</html>